﻿using FarPoint.Web.Spread;
using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.Formulas
{
    public partial class Overview : SpreadDemoPage
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitSpread();
                SetFormulas();
                fpSpread1.ClientAutoCalculation = true;
                this.fpSpread1.UseClipboard = false;
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        public void SetFormulas()
        {
            //Set up the formulas
            //Set up celltypes for formula cells
            fpSpread1.ActiveSheetView.Columns[2].CellType = new CurrencyCellType();
            fpSpread1.ActiveSheetView.Cells[4, 2, 4, 5].CellType = new CurrencyCellType();

            //Set row formulas
            fpSpread1.ActiveSheetView.Cells[0, 2].Formula = "SUM(D1:F1)";
            fpSpread1.ActiveSheetView.Cells[1, 2].Formula = "SUM(D2:F2)";
            fpSpread1.ActiveSheetView.Cells[2, 2].Formula = "SUM(D3:F3)";
            fpSpread1.ActiveSheetView.Cells[3, 2].Formula = "SUM(D4:F4)";

            //Set column formulas
            fpSpread1.ActiveSheetView.Cells[4, 2].Formula = "SUM(C1:C4)";
            fpSpread1.ActiveSheetView.Cells[4, 3].Formula = "SUM(D1:D4)";
            fpSpread1.ActiveSheetView.Cells[4, 4].Formula = "SUM(E1:E4)";
            fpSpread1.ActiveSheetView.Cells[4, 5].Formula = "SUM(F1:F4)";
        }

        public void InitSpread()
        {
            //Turn off paging
            fpSpread1.ActiveSheetView.AllowPage = false;

            //Set up styles
            fpSpread1.ActiveSheetView.ColumnHeader.DefaultStyle.Font.Name = "Verdana";
            fpSpread1.ActiveSheetView.ColumnHeader.DefaultStyle.Font.Size = FontUnit.XSmall;

            fpSpread1.ActiveSheetView.RowHeader.DefaultStyle.Font.Name = "Verdana";
            fpSpread1.ActiveSheetView.RowHeader.DefaultStyle.Font.Size = FontUnit.XSmall;

            fpSpread1.ActiveSheetView.DefaultStyle.Font.Name = "Verdana";
            fpSpread1.ActiveSheetView.DefaultStyle.Font.Size = FontUnit.XSmall;
            fpSpread1.ActiveSheetView.DefaultStyle.Font.Bold = false;

            fpSpread1.ActiveSheetView.ColumnCount = 6;
            fpSpread1.ActiveSheetView.RowCount = 5;

            //Set up the headers
            fpSpread1.ActiveSheetView.ColumnHeader.RowCount = 2;

            fpSpread1.ActiveSheetView.ColumnHeaderSpanModel.Add(0, 0, 2, 1);
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[0, 0].Text = "顾客";

            fpSpread1.ActiveSheetView.ColumnHeaderSpanModel.Add(0, 1, 2, 1);
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[0, 1].Text = "发票";

            fpSpread1.ActiveSheetView.ColumnHeaderSpanModel.Add(0, 2, 2, 1);
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[0, 2].Text = "总价";

            fpSpread1.ActiveSheetView.ColumnHeaderSpanModel.Add(0, 3, 1, 3);
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[0, 3].Text = "应收款";
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[0, 3].Font.Italic = true;
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[0, 3].Font.Bold = true;
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[0, 3].Font.Name = "Verdana";
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[0, 3].Font.Size = FontUnit.XSmall;

            fpSpread1.ActiveSheetView.ColumnHeader.Cells[1, 3].Text = "0-30";
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[1, 4].Text = "31-60";
            fpSpread1.ActiveSheetView.ColumnHeader.Cells[1, 5].Text = "61+";

            //Add Sample data
            //Customer
            //Merge rows
            fpSpread1.ActiveSheetView.SpanModel.Add(0, 0, 4, 1);
            fpSpread1.ActiveSheetView.Cells[0, 0].Text = "正人资源";
            fpSpread1.ActiveSheetView.Cells[0, 0].HorizontalAlign = HorizontalAlign.Center;
            fpSpread1.ActiveSheetView.Cells[0, 0].VerticalAlign = VerticalAlign.Middle;
            fpSpread1.ActiveSheetView.Cells[0, 0].Font.Bold = true;
            fpSpread1.ActiveSheetView.Cells[0, 0].Font.Name = "Verdana";
            fpSpread1.ActiveSheetView.Cells[0, 0].Font.Size = FontUnit.XSmall;

            //Order number
            fpSpread1.ActiveSheetView.Cells[0, 1].Text = "10365";
            fpSpread1.ActiveSheetView.Cells[1, 1].Text = "10507";
            fpSpread1.ActiveSheetView.Cells[2, 1].Text = "10535";
            fpSpread1.ActiveSheetView.Cells[3, 1].Text = "10573";

            //Date
            fpSpread1.ActiveSheetView.Cells[0, 1].Text = "10365";
            fpSpread1.ActiveSheetView.Cells[1, 1].Text = "10507";
            fpSpread1.ActiveSheetView.Cells[2, 1].Text = "10535";
            fpSpread1.ActiveSheetView.Cells[3, 1].Text = "10573";

            //Add amounts
            AddAmount(0, 3, 503.2);
            AddAmount(0, 4, 0);
            AddAmount(0, 5, 0);

            AddAmount(1, 3, 0);
            AddAmount(1, 4, 0);
            AddAmount(1, 5, 1648.5);

            AddAmount(2, 3, 0);
            AddAmount(2, 4, 395.2);
            AddAmount(2, 5, 0);

            AddAmount(3, 3, 542.5);
            AddAmount(3, 4, 0);
            AddAmount(3, 5, 0);

            //Change cell color
            fpSpread1.ActiveSheetView.Cells[1, 5].BackColor = Color.Red;
            fpSpread1.ActiveSheetView.Cells[1, 5].ForeColor = Color.Yellow;

            //Set up Columns styles
            int fpwid = 83;
            fpSpread1.ActiveSheetView.Columns[2].BackColor = Color.NavajoWhite;
            fpSpread1.ActiveSheetView.Columns[3, 5].BackColor = Color.Gainsboro;

            fpSpread1.ActiveSheetView.Columns[0].Width = 95;
            fpSpread1.ActiveSheetView.Columns[1].Width = 65;
            fpSpread1.ActiveSheetView.Columns[2].Width = fpwid;
            fpSpread1.ActiveSheetView.Columns[3].Width = fpwid;
            fpSpread1.ActiveSheetView.Columns[4].Width = fpwid;
            fpSpread1.ActiveSheetView.Columns[5].Width = fpwid;

            //Add line border
            Border border;
            border = new Border();
            border.BorderStyleTop = BorderStyle.Solid;
            border.BorderColorTop = Color.Red;
            border.BorderSizeTop = 2;
            fpSpread1.ActiveSheetView.Cells[4, 2, 4, 5].Border = border;

            //Add border around total due cell
            border = new Border();
            border.BorderColor = Color.Red;
            border.BorderSize = 2;
            border.BorderStyle = BorderStyle.Solid;
            fpSpread1.ActiveSheetView.Cells[4, 2].Border = border;
        }

        public void AddAmount(int row, int col, double amt)
        {
            fpSpread1.ActiveSheetView.Cells[row, col].Value = amt;
            fpSpread1.ActiveSheetView.Cells[row, col].CellType = new CurrencyCellType();
        }

    }
}